using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using vs.Core;

namespace Forum
{
    public partial class forum_breadcrumb : Window
    {
        private void ShowForum(string forumid, string forumdesk)
        {
            ForumController db = new ForumController();
            ForumInfo f = db.Load(forumid);

            string imgurl = ResolveUrl("~/images/folder.gif");
            lnkForum.Visible = true;
            lnkForum.Text = "<img src=\"" + imgurl + "\" border=\"0\">&nbsp;" + f.ForumName;
            lnkForum.NavigateUrl = UrlProvider.DesktopUrl(forumdesk, f.ForumID);

            string title = f.ForumName + "[ " + PortalUtils.SiteName + " ]";
            CommonFunction.ChangeHtmlTitle(title, title, title);
        }

        private void ShowTopic(int topicid, string forumdesk)
        {
            string strSQL = "SELECT t.TopicID, t.ForumID, t.TopicName, f.ForumName "
                + " FROM FORUM_Topic t INNER JOIN FORUM_Forum f ON t.ForumID = f.ForumID "
                + " WHERE (t.TopicID = @topicid)";
            DataParameter p = new DataParameter("@topicid", topicid);
            DataTable dt = DataFactory.SelectTable(strSQL, p);

            if(dt.Rows.Count > 0)
            {
                string blankurl = ResolveUrl("~/images/blank2.gif");
                string imgurl = ResolveUrl("~/images/folder.gif");
                lnkForum.Visible = true;
                lnkForum.Text = "<img src=\"" + imgurl + "\" border=\"0\">&nbsp;" + dt.Rows[0]["ForumName"].ToString();
                lnkForum.NavigateUrl = UrlProvider.DesktopUrl(forumdesk, dt.Rows[0]["ForumID"].ToString());

                lblTopicName.Visible = true;
                lblTopicName.Text = "<img src=\"" + blankurl + "\" border=\"0\">"
                    + "<img src=\"" + imgurl + "\" border=\"0\">&nbsp;"
                    + dt.Rows[0]["TopicName"].ToString();

                string title = dt.Rows[0]["TopicName"].ToString() + " :: " + PortalUtils.SiteName;
                CommonFunction.ChangeHtmlTitle(title, title, title);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    string homedesk = this.GetModuleSetting("homedesk");
                    string forumdesk = this.GetModuleSetting("forumdesk");
                    string topicdesk = this.GetModuleSetting("topicdesk");

                    string deskname = PortalUtils.CurrentDesktopName;
                    lnkHome.NavigateUrl = UrlProvider.DesktopUrl(homedesk);

                    if (deskname == forumdesk) ShowForum(Request.QueryString["ID"], forumdesk);
                    if (deskname == topicdesk) ShowTopic(Utils.RequestID(), forumdesk);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}